Ext.namespace("Meeting");

Meeting.Add = function() {
	this.userSelector = null;
	this.selectUserId = [];
	this.users=[];
	this.userType=0;//1:负责人 2:参与人
	this.mtId=new Ext.form.Hidden({});
	this.mtName = new Ext.form.TextField({
		fieldLabel: '会议名称',
		width: 500,
		allowBlank: false,
		blankText: '请填写会议名称',
		maxLength:100
	});
	this.mtRoom=new Ext.form.ComboBox({
			width: 203,
			fieldLabel:'会议室',
			displayField:'text',
			valueField:'value',
			allowBlank: false,
			mode:'local',
			triggerAction: 'all',
			editable: false,
			emptyText:'请选择',						
			store: new Ext.data.SimpleStore({
			fields:['value','text'],
			data:OMS.MT_ROOM
			})
		});
	this.mtStartDate = new Ext.ux.form.DateTime({
		fieldLabel: '开始时间',
		timeFormat:'H:i',
        timeConfig: {
            altFormats:'H:i',
            allowBlank: false,
            blankText: '请填写会议开始具体时间'
        },
        dateFormat:'Y-m-d',
        dateConfig: {
            altFormats:'Y-m-d',
            allowBlank: false,
            blankText: '请填写会议开始日期'
        }        
	});
	this.mtEndDate = new Ext.ux.form.DateTime({
		fieldLabel: '结束时间',
		timeFormat:'H:i',
        timeConfig: {
            altFormats:'H:i',
            allowBlank: false,
            blankText: '请填写会议结束时间'
        },
        dateFormat:'Y-m-d',
        dateConfig: {
            altFormats:'Y-m-d',
            allowBlank: false,
            blankText: '请填写会议结束日期'
        }
	});
	this.mtJoiner = new Ext.form.TextField({
		fieldLabel: '参与人员',
		readOnly: true,
		allowBlank: false,
		width: 205,
		blankText: '请选择执行人',
		listeners: {
		   'render': function(cmp) {
				var self = cmp.ownerCt.ownerCt.ownerCt;
				cmp.getEl().on('click', function(){ self.showSelector('2')}, self); 
			}
		}
	});
	this.mtAssigner = new Ext.form.TextField({
		fieldLabel: '负责人员',
		readOnly: true,
		allowBlank: false,
		width: 205,
		blankText: '请选择负责人',
		listeners: {
		   'render': function(cmp) {
				var self = cmp.ownerCt.ownerCt.ownerCt;
				cmp.getEl().on('click',function(){ self.showSelector('1')}, self); 
			}
		}
	});
	this.mtNotice = new Ext.form.TextArea({
		fieldLabel: '会议说明',
		width: 500,
		maxLength:250
	});
	this.mtLevel=new Ext.form.ComboBox({
			width: 203,
			fieldLabel:'会议规格',
			editable: false,
			displayField:'text',
			valueField:'value',
			mode:'local',
			emptyText:'请选择',						
			store: new Ext.data.SimpleStore({
			fields:['value','text'],
			editable: false,
			data:OMS.MT_LEVEL
			})
		});
	this.mtAttention = new Ext.form.CheckboxGroup({
		width:120,
		fieldLabel: '会议提醒',
		items: [
		      {boxLabel: '短信', name: 'smsRemind',inputValue:'1'},
		      {boxLabel: '邮件', name: 'emailRemind',inputValue:'2'}
		]
	});
	this.mtSummary = new Ext.form.HtmlEditor({
        fieldLabel: '会议纪要',
        enableAlignments: true,
        enableColors: true,
        enableFont: true,
        enableFontSize: true,
        enableFormat: true,
        enableLinks: true,
        enableLists: true,
        enableSourceEdit: true,
        fontFamilies: ['宋体', '黑体']
    });
	
	this.btnSearch = new Ext.Toolbar.Button({
	        iconCls: 'icon-search',
	        text: '查看今日会议',
	        tooltip: {text: '查看今日会议'},
	        scope: this,
	        handler:this.searchMeeting
	    });
	this.btnSave = new Ext.Button({
            text: '保存',
            cls: 'x-btn-text-icon btn-save',
            handler: this.save,
            scope: this
    });
	this.btnToTask = new Ext.Button({
            text: '派发任务',
            cls: 'x-btn-text-icon btn-save',
            scope: this,            
            handler:this.toTask            
    });
	this.btnToDuban = new Ext.Button({
            text: '转督办',
            cls: 'x-btn-text-icon btn-save',
            scope: this.toDuban
        });
	this.btnDownload = new Ext.Button({
            text: '下载会议文件',
            cls: 'x-btn-text-icon btn-save',
            scope: this.download
        });
	this.btnUpload = new Ext.Button({
            text: '上传会议文件',
            cls: 'x-btn-text-icon btn-save',
            scope: this.upload
    });
	this.form = new Ext.form.FormPanel({
        bodyStyle: 'padding: 5px 5px 0',
        frame:true,
        Height: 600,
        width: 680,
        labelAlign: 'right',
        labelWidth: 60,
        layout:'table',
        layoutConfig:{columns:2},
        items: [
                {colspan:2,layout:'form',items:[this.mtName]},
                {layout:'form',items: [this.mtRoom]},
                {layout:'form',items: [this.btnSearch]},
                {layout:'form', items:[this.mtStartDate]},
                {layout:'form', items:[this.mtEndDate]},
                {layout:'form', items:[this.mtAssigner]},
                {layout:'form', items:[this.mtJoiner]},
                {layout:'form', items:[this.mtLevel]},
                {layout:'form', items:[this.mtAttention]},
                {colspan:2, layout:'form', items:[this.mtNotice]},
                {colspan:2, layout:'form', items:[this.mtSummary]}
		]
    });
	
	Meeting.Add.superclass.constructor.call(this,{
		title: '会议安排',
        bodyStyle: 'padding: 1px 1px 0',
        width: 700,
        resizable: false,
        modal: true,
        frame:true,
        closeAction: 'hide',
        items: [this.form],
        buttons: [this.btnSave,this.btnToTask,this.btnToDuban,this.btnDownload,this.btnUpload,{
            text: '关闭',
            cls: 'x-btn-text-icon btn-cancel',
            handler: this.hide.createDelegate(this, [])
        }]
	});	
};

Ext.extend(Meeting.Add, Ext.Window, {		
	searchMeeting:function(){
		Meeting.Today.instance = new Meeting.Today();
		Meeting.Today.instance.initData(this.mtStartDate.getValue());
		Meeting.Today.instance.show();				
	},
	initEdit:function(data){
		//会议开始之前不能派发任务,转督办,
		if(data.MT_START_DATE>new Date().format('Y-m-d H:i')){
			this.btnToTask.disabled=true;
			this.btnToDuban.disabled=true;
			this.mtSummary.disabled=true;
			//this.btnDownload.disabled=true;
			//this.btnUpload.disabled=true;
		}
		//会议开始之后,除会议纪要可以编辑之外,其他的信息都不能编辑
		else if(data.MT_START_DATE<new Date().format('Y-m-d H:i')){
			this.mtId.disabled=true;
			this.mtName.disabled=true;
			this.mtRoom.disabled=true;
			this.mtStartDate.disabled=true;
			this.mtEndDate.disabled=true;
			this.mtJoiner.disabled=true;
			this.mtAssigner.disabled=true;
			this.mtNotice.disabled=true;
			this.mtLevel.disabled=true;
			this.mtAttention.disabled=true;
			this.mtSummary.disabled=false;
		}
		this.mtId.setValue(data.MT_ID);
		this.mtName.setValue(data.MT_NAME);
		this.mtRoom.setValue(data.MT_ROOM);
		this.mtStartDate.setValue(Date.parseDate(data.MT_START_DATE,'Y-m-d H:i'));
		this.mtEndDate.setValue(Date.parseDate(data.MT_END_DATE,'Y-m-d H:i'));
		this.mtJoiner.setValue(data.MT_JOINER);
		this.mtAssigner.setValue(data.MT_ASSIGNER);
		this.mtNotice.setValue(data.MT_NOTICE);
		this.mtLevel.setValue(data.MT_LEVEL);
		if(data.MT_ATTENTION==1){
			this.mtAttention.items[0].checked=true;
		}
		if(data.MT_ATTENTION==2){
			this.mtAttention.items[1].checked=true;
		}
		if(data.MT_ATTENTION==3){
			this.mtAttention.items[0].checked=true;
			this.mtAttention.items[1].checked=true;
		}
		//this.mtAttention.setValue(data.MT_ATTENTION);
		this.mtSummary.setValue(data.MT_SUMMARY);
	},	
	toTask:function(){
		if(!Task.Add.win.instance)
			Task.Add.win.instance = new Task.Add.win();
		Task.Add.win.instance.show();
		Task.Add.win.instance.initAdd();
	},
	toDuban:function(){},
	download:function(){},
	upload:function(){},
	save: function(){
		if(!this.form.getForm().isValid()){
			Utils.info("请将信息填写完整!","系统提示");
			return;
		}	
		//修改会议信息
		if(this.mtStartDate.getValue()>new Date().format('Y-m-d H:i')){
			if(this.mtStartDate.getValue()<new Date()){
				Utils.info("会议开始时间不能小于当前时间！","系统提示");
				return;
			}
			if(this.mtStartDate.getValue()>this.mtEndDate.getValue()){
				Utils.info("会议开始时间不能大于会议结束时间！","系统提示");
				return;
			}
		}
		if(this.mtAssigner.getValue().length>5){
			Utils.info("会议负责人只能选择一个！","系统提示");
			this.mtAssigner.setValue("");
			return;
		}
		Ext.Ajax.request({
					url: '../meeting/editMeeting.form?timesnap='+Date.parse(new Date()),
					method: 'POST',
					scope:this,
					params: {
						mtId : this.mtId.getValue(),
						mtName  : this.mtName.getValue(),
						mtRoom : this.mtRoom.getValue(),
						mtStartDate : this.mtStartDate.getValue().format('Y-m-d H:i'),
						mtEndDate : this.mtEndDate.getValue().format('Y-m-d H:i'),
						mtAssigner : this.mtAssigner.getValue(),
						mtJoiner : this.mtJoiner.getValue(),
						mtSummary : this.mtSummary.getValue(),
						mtLevel : this.mtLevel.getValue(),
						mtAttention : this.mtAttention.getRawValue(),
						mtNotice : this.mtNotice.getValue(),
						userId: this.selectUserId.join(',')
					},
					success:function(resp){						
						//Meeting.Add.instance.form.getForm().reset();
		            	this.hide();
		            	Meeting.List.instance.searchMeeting();	
		            	Utils.info("操作成功！","系统提示");				
					},
					failure:function(resp){
						var data = Ext.util.JSON.decode(resp.responseText);
			            if (data && data.msg) {
			            	Utils.info(data.msg);
			            }else{
							Utils.info("操作失败!"+data,"系统提示");
						}
					}
		});
	},
	showSelector: function(text) {
		this.userType=text;
		if (!this.userSelector) {
			this.userSelector = new Selector.User(this, '执行人');
		}
		//this.userSelector.show([{id: '22', name: '史徐华'},{id:33, name:'shadow'}]);
		this.userSelector.initUsers(this.users);
		this.userSelector.show();
	},
	setUsers: function(o) {	// for Selector.User callback
		this.users = o;
		var names = [];
		this.selectUserId =[];
		for(var i=0, u; u = this.users[i++];) {
			names.push(u.name);
			this.selectUserId.push(u.id);
		}
		if(this.userType==1){
			this.mtAssigner.setValue(names.join(';'));
		}
		if(this.userType==2){
			this.mtJoiner.setValue(names.join(';'));
		}
	}
});
